package com.somsoft.unlockme.db;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class Service {
	private DBOpenHelper helper;
	
	public Service(Context context){
		this.helper = new DBOpenHelper(context);
	}
	
	/**
	 * 添加记录
	 * @param person
	 */
	public void add(Level level){
		SQLiteDatabase  db = helper.getWritableDatabase();
		try {
			db.beginTransaction();
			db.execSQL("insert into t_level(level,isLock,score,isGetScore,step,best,totalScore) values(?,?,?,?,?,?,?) ",
					new Object[]{level.getLevel(),level.getIsLock(),level.getScore(),level.getIsGetScore(),level.getStep(),level.getBest(),level.getTotalScore()});
			db.setTransactionSuccessful();  
		} finally {
			db.endTransaction();
			db.close();
		} 
	}
	
	/**
	 * 过关奖励，每次1分
	 */
	public void addScore(){
		SQLiteDatabase  db = helper.getWritableDatabase();
		try {
			db.beginTransaction();
			db.execSQL("update t_level set totalScore = totalScore+1",new Object[]{});
			db.setTransactionSuccessful();  
		} finally {
			db.endTransaction();
			db.close();
		} 
	}
	
	
	/**
	 * 跳关，每次扣10分
	 */
	public void cuteScore(){
		SQLiteDatabase  db = helper.getWritableDatabase();
		try {
			db.beginTransaction();
			db.execSQL("update t_level set totalScore = totalScore-10",new Object[]{});
			db.setTransactionSuccessful();  
		} finally {
			db.endTransaction();
			db.close();
		} 
	}
	
	/**
	 * 获取总积分
	 */
	public int getScore(){
		int totalScore = 0;
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor =  db.rawQuery("select * from t_level where level = ?",new String[]{"1"});
		if(cursor.moveToFirst()){
			totalScore = cursor.getInt(cursor.getColumnIndex("totalScore"));
		}
		cursor.close();
		db.close();
		return totalScore;
	}
	
	/**
	 * 更新记录
	 * @param person 更新的记录
	 */
	public void update(Level level){
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			db.beginTransaction();
			db.execSQL("update t_level set isLock=?,score=?,isGetScore=?,step=?,best=?,totalScore=? where level = ?",
					new Object[]{level.getIsLock(),level.getScore(),level.getIsGetScore(),level.getStep(),level.getBest(),level.getTotalScore(),level.getLevel()});
			db.setTransactionSuccessful();  
		} finally {
			db.endTransaction();
			db.close();
		} 
	}
	
	/**
	 * 查找记录
	 * @param integer
	 * @return
	 */
	public Level find(Integer level){
		Level obj = null;
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor =  db.rawQuery("select * from t_level where level = ?",
										new String[]{level.toString()});
		if(cursor.moveToFirst()){
			int lv = cursor.getInt(cursor.getColumnIndex("level"));	
			String isLock = cursor.getString(cursor.getColumnIndex("isLock"));
			int score = cursor.getInt(cursor.getColumnIndex("score"));
			String isGetScore = cursor.getString(cursor.getColumnIndex("isGetScore"));
			int step = cursor.getInt(cursor.getColumnIndex("step"));
			int best = cursor.getInt(cursor.getColumnIndex("best"));
			int totalScore = cursor.getInt(cursor.getColumnIndex("totalScore"));
			
			obj = new Level(lv,isLock,score,isGetScore,step,best,totalScore); 
		}
		cursor.close();
		db.close();
		return obj;
	}
	
	/**
	 * 分页显示记录
	 * @param start 开始记录号
	 * @param length 记录长度
	 * @return
	 */
	public List<Level> getLevels(Integer start, Integer length){
		List<Level> levels = new ArrayList<Level>();
		Level level = null;
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor =  db.rawQuery("select * from t_level order by level limit ?, ?",
										new String[]{start.toString(),length.toString()});
		while(cursor.moveToNext()){
			int lv = cursor.getInt(cursor.getColumnIndex("level"));	
			String isLock = cursor.getString(cursor.getColumnIndex("isLock"));
			int score = cursor.getInt(cursor.getColumnIndex("score"));
			String isGetScore = cursor.getString(cursor.getColumnIndex("isGetScore"));
			int step = cursor.getInt(cursor.getColumnIndex("step"));
			int best = cursor.getInt(cursor.getColumnIndex("best"));
			int totalScore = cursor.getInt(cursor.getColumnIndex("totalScore"));
			
			level = new Level(lv,isLock,score,isGetScore,step,best,totalScore); 
			levels.add(level);
			level = null;
		}
		cursor.close();
		db.close();
		return levels;
	}
	
}